package com.ihd.ihardware.view.tzc.me.view;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.app.MyApplication;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivitySettingBinding;
import com.ihd.ihardware.pojo.MembersRes;
import com.ihd.ihardware.pojo.UpdateRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.enter.view.PerActivity;
import com.ihd.ihardware.view.tzc.me.viewmodel.SettingViewModel;
import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.jpush.android.api.JPushInterface;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.AppActivityManager;
import cn.wowjoy.commonlibrary.utils.DataCleanManager;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.SysUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class SettingActivity extends BaseActivity<ActivitySettingBinding, SettingViewModel> {

    private UserInfoRes.DataBean ud;
    private MDialog dg;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("设置");
        binding.mtitlebar.setLeftBack(this);

        try {
            binding.idTV.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
            binding.versionTV.setText(SysUtils.getVersionName());
        } catch (Exception e) {
        }

        binding.changePsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SettingActivity.this, ChangeActivity.class);
                startActivity(i);
            }
        });

        binding.about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SettingActivity.this, AboutActivity.class);
                startActivity(i);
            }
        });

        binding.update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.checkUpdate();
            }
        });

        binding.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dg = DialogUtils.alertDialog(SettingActivity.this, MDialog.DG_TYPE.ALERT, "是否清除缓存?", "取消", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dg.cancel();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dg.cancel();
                        DataCleanManager.clearAllCache(getApplicationContext());
                        try {
                            binding.idTV.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
                        } catch (Exception e) {
                        }
                    }
                });
            }
        });

        binding.logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dg = DialogUtils.alertDialog(SettingActivity.this, MDialog.DG_TYPE.ALERT, "是否确定退出?", "取消", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dg.cancel();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dg.cancel();
                        JPushInterface.deleteAlias(SettingActivity.this, 0);
                        SPUtils.removeAll();
                        MyApplication.memberId = new MembersRes.DataBean();
                        MyApplication.membersData.clear();
                        Intent i = new Intent(SettingActivity.this, PerActivity.class);
                        startActivity(i);
                        AppActivityManager.getInstance().killAllActivity(PerActivity.class);
                    }
                });
            }
        });

        setRx(AppConstans.MUPGRADE, new BaseConsumer<UpdateRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(UpdateRes updateInfo) {
                if (updateInfo.getData().getVersionNumber() > SysUtils.getVersionCode()) {
                    dg = DialogUtils.alertDialog(SettingActivity.this, MDialog.DG_TYPE.ALERT, updateInfo.getData().getDescription(), "以后再说", "立即更新",
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dg.cancel();
                                }
                            }, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dg.cancel();
                                    ToastUtils.showLong(SettingActivity.this, "正在下载...");
                                    viewModel.download(updateInfo);
                                }
                            });
                } else {
                    ToastUtils.showLong(SettingActivity.this, "已经是最新版啦");
                }
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(SettingActivity.this, error.getMessage());
            }
        });

        LiveEventBus.get().with(AppConstans.M_DOWNLOAD_APK, UpdateRes.class).observe(this, new Observer<UpdateRes>() {
            @Override
            public void onChanged(@Nullable UpdateRes s) {
                SysUtils.installApk(SettingActivity.this, AppConstans.UPDATE_PATH);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected Class getViewModelClass() {
        return SettingViewModel.class;
    }
}
